cat('Creating Tables A17-A20 \n\n')

dat = read.csv('TablesA17A18A19A20_data.csv')

dat1 = dat[dat$study==1,]  ##DLABSS sample
dat2 = dat[dat$study==2,]  ##Qualtrics sample

dat1.nonsouth = dat[dat$study==1&dat$south==0,]
dat2.nonsouth = dat[dat$study==2&dat$south==0,]


reg1 = lm(pse~dissimilarity+percent.black,
          data = dat1)
clustered.se = cl(dat1, reg1, dat1$metroid)
reg1$se = clustered.se

reg2 = lm(pse~dissimilarity*percent.black,
          data = dat1)
clustered.se = cl(dat1, reg2, dat1$metroid)
reg2$se = clustered.se

reg3 = lm(pse~dissimilarity+percent.black+I(percent.black^2),
          data = dat1)
clustered.se = cl(dat1, reg3, dat1$metroid)
reg3$se = clustered.se

reg4 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat1)
clustered.se = cl(dat1, reg4, dat1$metroid)
reg4$se = clustered.se

reg5 = lm(pse~dissimilarity+percent.black+income.recode+college.educated+contact,
          data = dat1)
clustered.se = cl(dat1, reg5, dat1$metroid)
reg5$se = clustered.se

reg6 = lm(pse~dissimilarity*percent.black+income.recode+college.educated+contact,
          data = dat1)
clustered.se = cl(dat1, reg6, dat1$metroid)
reg6$se = clustered.se

reg7 = lm(pse~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated+contact,
          data = dat1)
clustered.se = cl(dat1, reg7, dat1$metroid)
reg7$se = clustered.se

reg8 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated+contact,
          data = dat1)
clustered.se = cl(dat1, reg8, dat1$metroid)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated','Contact')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     notes = '',
                     stars = 'default'
    )
writeLines(
  outtable, 'TableA17.tex')


#########
reg1 = lm(pse~dissimilarity+percent.black,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg1, dat1.nonsouth$metroid)
reg1$se = clustered.se

reg2 = lm(pse~dissimilarity*percent.black,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg2, dat1.nonsouth$metroid)
reg2$se = clustered.se

reg3 = lm(pse~dissimilarity+percent.black+I(percent.black^2),
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg3, dat1.nonsouth$metroid)
reg3$se = clustered.se

reg4 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg4, dat1.nonsouth$metroid)
reg4$se = clustered.se

reg5 = lm(pse~dissimilarity+percent.black+income.recode+college.educated+contact,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg5, dat1.nonsouth$metroid)
reg5$se = clustered.se

reg6 = lm(pse~dissimilarity*percent.black+income.recode+college.educated+contact,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg6, dat1.nonsouth$metroid)
reg6$se = clustered.se

reg7 = lm(pse~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated+contact,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg7, dat1.nonsouth$metroid)
reg7$se = clustered.se

reg8 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated+contact,
          data = dat1.nonsouth)
clustered.se = cl(dat1.nonsouth, reg8, dat1.nonsouth$metroid)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated','Contact')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'
)
writeLines(
  outtable, 'TableA19.tex')


##############
reg1 = lm(pse~dissimilarity+percent.black,
          data = dat2)
clustered.se = cl(dat2, reg1, dat2$metroid)
reg1$se = clustered.se

reg2 = lm(pse~dissimilarity*percent.black,
          data = dat2)
clustered.se = cl(dat2, reg2, dat2$metroid)
reg2$se = clustered.se

reg3 = lm(pse~dissimilarity+percent.black+I(percent.black^2),
          data = dat2)
clustered.se = cl(dat2, reg3, dat2$metroid)
reg3$se = clustered.se

reg4 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat2)
clustered.se = cl(dat2, reg4, dat2$metroid)
reg4$se = clustered.se

reg5 = lm(pse~dissimilarity+percent.black+income.recode+college.educated+contact,
          data = dat2)
clustered.se = cl(dat2, reg5, dat2$metroid)
reg5$se = clustered.se

reg6 = lm(pse~dissimilarity*percent.black+income.recode+college.educated+contact,
          data = dat2)
clustered.se = cl(dat2, reg6, dat2$metroid)
reg6$se = clustered.se

reg7 = lm(pse~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated+contact,
          data = dat2)
clustered.se = cl(dat2, reg7, dat2$metroid)
reg7$se = clustered.se

reg8 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated+contact,
          data = dat2)
clustered.se = cl(dat2, reg8, dat2$metroid)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated','Contact')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'
)
writeLines(
  outtable, 'TableA18.tex')


###############
reg1 = lm(pse~dissimilarity+percent.black,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg1, dat2.nonsouth$metroid)
reg1$se = clustered.se

reg2 = lm(pse~dissimilarity*percent.black,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg2, dat2.nonsouth$metroid)
reg2$se = clustered.se

reg3 = lm(pse~dissimilarity+percent.black+I(percent.black^2),
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg3, dat2.nonsouth$metroid)
reg3$se = clustered.se

reg4 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg4, dat2.nonsouth$metroid)
reg4$se = clustered.se

reg5 = lm(pse~dissimilarity+percent.black+income.recode+college.educated+contact,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg5, dat2.nonsouth$metroid)
reg5$se = clustered.se

reg6 = lm(pse~dissimilarity*percent.black+income.recode+college.educated+contact,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg6, dat2.nonsouth$metroid)
reg6$se = clustered.se

reg7 = lm(pse~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated+contact,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg7, dat2.nonsouth$metroid)
reg7$se = clustered.se

reg8 = lm(pse~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated+contact,
          data = dat2.nonsouth)
clustered.se = cl(dat2.nonsouth, reg8, dat2.nonsouth$metroid)
reg8$se = clustered.se

coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated','Contact')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     notes = '',
                     stars = 'default'
    )
writeLines(
  outtable, 'TableA20.tex')

##changes
cat('First differences reported on page 89 of book: \n')
z5 <- zls$new()
z5$zelig(pse~dissimilarity+percent.black+income.recode+college.educated+contact, 
         data = dat1.nonsouth)


z5$setx(dissimilarity  = .15, percent.black = .11)
z5$setx1(dissimilarity  = .77, percent.black = .11)
z5$sim()
my.fd <- z5$getqi(qi="fd", xvalue="x1")

x.predict <- z5$getqi(qi="ev", xvalue="x")
x1.predict <- z5$getqi(qi="ev", xvalue="x1")

cat(paste('mean = ',round(mean(my.fd),3),sep=''))
cat('\n 95% CI:\n')
print(quantile(my.fd,c(.025,.975)))





